package passwordEncryption;

import java.util.Scanner;
//已修改
public class Encryption {
    public static String encryption(String password){
        char[] psArray = new char[password.length()];
        for (int i = 0; i < password.length(); i++){
            psArray[i] = (char)(password.charAt(i)+(i+1)+3);   //(i+1)是字符串上的位置，3是偏移值
        }
        char temp;
        //将字符串的第一位和最后一位调换顺序
        temp = psArray[password.length()-1];
        psArray[password.length()-1] = psArray[0];
        psArray[0] = temp;
        //将字符串反转
        String beString = String.valueOf(psArray);
        StringBuffer rv = new StringBuffer(beString);
        rv.reverse();
        String result = String.valueOf(rv);
        return result;
    }

    public static String decode(String encryptionPs){
        //字符串反转
        StringBuffer rv = new StringBuffer(encryptionPs);
        rv.reverse();
        String rs = String.valueOf(rv);
        //字符串的第一位和最后一位调换顺序
//        char[] psArray = new char[encryptionPs.length()];
        char[] psArray = rs.toCharArray();
        char temp = psArray[encryptionPs.length()-1];
        psArray[encryptionPs.length()-1] = psArray[0];
        psArray[0] = temp;
        for (int i = encryptionPs.length()-1; i > 0; i--){
//            psArray[i] = (char)(encryptionPs.charAt(i)-(i+1)-3);   //(i+1)是字符串上的位置，3是偏移值
            psArray[i] = (char)(psArray[i]-(i+1)-3);   //(i+1)是字符串上的位置，3是偏移值
        }
        String result = String.valueOf(psArray);
        return result;
    }



    public static void main(String[] args) {
        String ps = "fwk123";
        System.out.println(encryption(ps));
        System.out.println("1");
        System.out.println(decode(encryption(ps)));
    }
}
